﻿using XQ.RequestOfficeSupplies.DAL;
using XQ.RequestOfficeSupplies.DAL.DAL;
using XQ.RequestOfficeSupplies.DAL.Model;
using System.Collections.Generic;
using XQ.RequestOfficeSupplies.DAL.Model.Sys;

namespace XQ.RequestOfficeSupplies.BLL.Sys
{
    public class RightMangerBll
    {
        private RoleMenusDal _roleMenus;
        private readonly RoleDal _roleDal = new RoleDal();

        /// <summary>
        ///  获了菜单按钮
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="menuId">菜单ID</param>
        /// <returns></returns>
        public List<RoleMenuModel> GetMenuButtons(int roleId, int menuId)
        {
            _roleMenus = new RoleMenusDal();
            return _roleMenus.GetMenuButtons(roleId, menuId);
        }

        /// <summary>
        /// 根据角色ID获取所有对应的菜单权限
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="menuId">菜单ID</param>
        /// <returns></returns>
        public SaveMenusButtonsModel GetMenuButtonsByRoleID(int roleId)
        {
            SaveMenusButtonsModel saveMenusButtonsModel = new SaveMenusButtonsModel();
            _roleMenus = new RoleMenusDal();
            saveMenusButtonsModel.RoleId = roleId;
            saveMenusButtonsModel.MenuIDs= _roleMenus.GetMenuIDsByRoleID(roleId);
            saveMenusButtonsModel.HalfMenuIDs = _roleMenus.GetMenuIDsByRoleID(roleId, true);

            return saveMenusButtonsModel;
        }

        /// <summary>
        /// 获取角色的页面按钮
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="menuId">菜单ID</param>
        /// <param name="buttonId"></param>
        /// <returns></returns>
        public bool GetRoleButton(int roleId, int menuId, int buttonId)
        {
            _roleMenus = new RoleMenusDal();
            return _roleMenus.GetRoleButton(roleId, menuId, buttonId);
        }
        /// <summary>
        /// 保存角色权限分配
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="menuId">菜单ID</param>
        /// <param name="btnId">菜单ID</param>
        /// <param name="flag">标记</param>
        public void SaveRights(int roleId, int menuId, int btnId, bool flag)
        {
            _roleMenus = new RoleMenusDal();
            _roleMenus.SaveRights(roleId, menuId, btnId, flag);
        }

        /// <summary>
        /// 角色分配权限
        /// </summary>
        /// <param name="roleId">权限ID </param>
        /// <param name="MenuIDs">菜单ID数组</param>
        /// <returns></returns>
        public bool SaveMenusButtons(int roleId, List<int> menuIDs, List<int> halfMenuIDs)
        {
            _roleMenus = new RoleMenusDal();
            return _roleMenus.SaveRights(roleId, menuIDs, halfMenuIDs);
        }

        /// <summary>
        /// 检查角色与菜单关系
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="menuId">菜单ID</param>
        /// <returns></returns>
        public bool CheckRoleMenu(int roleId, int menuId)
        {
            _roleMenus = new RoleMenusDal();
            return _roleMenus.CheckRoleMenu(roleId, menuId);
        }

        /// <summary>
        ///获取Role根据用户ID
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public Sys_Roles GetRolesByUserId(int userId)
        {
            return _roleDal.GetRoleByUserId(userId);
        }


        public void DeleteRights(int roleId, int menuId)
        {
            _roleMenus = new RoleMenusDal();
            _roleMenus.DleteRights(roleId, menuId);
        }
    }
}
